"""
配置信息
"""

# 获取项目根目录
import os
import sys
import logging


# logger = common.get_logger("settings")

BASE_DIR = os.path.dirname(os.path.dirname(__file__))


CONFIG_PATH = os.path.join(BASE_DIR, "settings.cfg")

# 日志的存放目录
LOG_DIR = os.path.join(BASE_DIR, "logs")


def get_logger(logger_name):

    # 创建一个Logger实例
    logger = logging.getLogger(logger_name)
    # 设置日志级别
    logger.setLevel(logging.DEBUG)

    # 设置日志存放路径
    base_path = LOG_DIR + "\\"
    log_path = base_path + logger_name + ".log"
    fh = logging.FileHandler(log_path, encoding="utf-8")

    # 设置日志格式
    formatter = logging.Formatter(
        "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
    )
    fh.setFormatter(formatter)

    logger.addHandler(fh)  # 将日志输出到文件

    return logger


logger = get_logger("settings")
if not os.path.exists(CONFIG_PATH):
    logger.critical("配置文件不存在，请检查！")
    exit()


import configparser

config = configparser.ConfigParser()
config.read(
    CONFIG_PATH, encoding="utf-8-sig"
)  # 使用utf-8-sig编码读取配置文件，避免签名问题


USER_DATA_DIR = config.get("path", "USER_DATA_DIR")
if not os.path.exists(USER_DATA_DIR):  # 如果不存在，则创建
    USER_DATA_DIR = os.path.join(BASE_DIR, "db", "user_data")


GOODS_DATA_DIR = config.get("path", "GOODS_DATA_DIR")
if not os.path.exists(GOODS_DATA_DIR):  # 如果不存在，则创建
    GOODS_DATA_DIR = os.path.join(BASE_DIR, "db", "goods_data")


# 获取提现手续费率
RATE = config.getfloat("bank", "RATE")


if __name__ == "__main__":
    print(BASE_DIR, type(BASE_DIR))
    print(USER_DATA_DIR, type(USER_DATA_DIR))
    print(GOODS_DATA_DIR, type(GOODS_DATA_DIR))
    print(LOG_DIR, type(LOG_DIR))
    print(sys.path)
